Completed
Pull Request — develop (#209)
by Xaver
33s
created

language.js ➔ ... ➔   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 73

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
c 3
b 0
f 0
nc 1
dl 0
loc 73
rs 9.0675
nop 0

6 Functions

Rating   Name   Duplication   Size   Complexity  
A �� getLocale 0 12 1
A �� setLocale 0 4 1
A �� languageSelect 0 13 2
A �� setTranslation 0 18 2
A �� setSelectLocale 0 3 1
A �� init 0 7 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
define(['polyglot', 'moment', 'helper'], function (Polyglot, moment, helper) {
2
  'use strict';
3
  return function () {
4
    var router;
5
6
    function languageSelect(el) {
7
      var select = document.createElement('select');
8
      select.className = 'language-switch';
9
      select.setAttribute('aria-label', 'Language');
10
      select.addEventListener('change', setSelectLocale);
11
      el.appendChild(select);
12
13
      // Keep english
14
      select.innerHTML = '<option>Language</option>';
15
      for (var i = 0; i < config.supportedLocale.length; i++) {
0 ignored issues
show
Bug introduced by
The variable config seems to be never declared. If this is a global, consider adding a /** global: config */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
16
        select.innerHTML += '<option value="' + config.supportedLocale[i] + '">' + config.supportedLocale[i] + '</option>';
17
      }
18
    }
19
20
    function setSelectLocale(event) {
21
      router.fullUrl({ lang: event.target.value }, false, true);
22
    }
23
24
    function setLocale(lang) {
25
      localStorage.setItem('language', getLocale(lang));
0 ignored issues
show
Bug introduced by
The variable localStorage seems to be never declared. If this is a global, consider adding a /** global: localStorage */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
26
      location.reload();
27
    }
28
29
    function getLocale(input) {
30
      var language = input || localStorage.getItem('language') || navigator.languages && navigator.languages[0] || navigator.language || navigator.userLanguage;
0 ignored issues
show
Bug introduced by
The variable navigator seems to be never declared. If this is a global, consider adding a /** global: navigator */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
Bug introduced by
The variable localStorage seems to be never declared. If this is a global, consider adding a /** global: localStorage */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
31
      var locale = config.supportedLocale[0];
1 ignored issue
show
Bug introduced by
The variable config seems to be never declared. If this is a global, consider adding a /** global: config */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
32
      config.supportedLocale.some(function (item) {
33
        if (language.indexOf(item) !== -1) {
34
          locale = item;
35
          return true;
36
        }
37
        return false;
38
      });
39
      return locale;
40
    }
41
42
    function setTranslation(json) {
43
      _.extend(json);
44
45
      if (moment.locale(_.locale()) !== _.locale()) {
46
        moment.defineLocale(_.locale(), {
47
          longDateFormat: {
48
            LT: 'HH:mm',
49
            LTS: 'HH:mm:ss',
50
            L: 'DD.MM.YYYY',
51
            LL: 'D. MMMM YYYY',
52
            LLL: 'D. MMMM YYYY HH:mm',
53
            LLLL: 'dddd, D. MMMM YYYY HH:mm'
54
          },
55
          calendar: json.momentjs.calendar,
56
          relativeTime: json.momentjs.relativeTime
57
        });
58
      }
59
    }
60
61
    function init(r) {
62
      router = r;
63
      /** global: _ */
64
      window._ = new Polyglot({ locale: getLocale(router.getLang()), allowMissing: true });
65
      helper.getJSON('locale/' + _.locale() + '.json?' + config.cacheBreaker).then(setTranslation);
1 ignored issue
show
Bug introduced by
The variable config seems to be never declared. If this is a global, consider adding a /** global: config */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
66
      document.querySelector('html').setAttribute('lang', _.locale());
67
    }
68
69
    return {
70
      init: init,
71
      getLocale: getLocale,
72
      setLocale: setLocale,
73
      languageSelect: languageSelect
74
    };
75
  };
76
});
77